﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="SettingsManager.cs" company="">
//   
// </copyright>
// <summary>
//   Contains the settings operations...
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Directory2Rss.Data
{
    using System;
    using System.Data.EntityClient;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Xml.Linq;

    /// <summary>
    /// Contains the settings operations...
    /// </summary>
    public static class SettingsManager
    {
        /// <summary>
        /// Initializes static members of the <see cref="SettingsManager"/> class.
        /// </summary>
        static SettingsManager()
        {
            // Create and Populate Variables from Config file
            string serverName = ReadConfigValue("SqlServer");
            string databaseName = ReadConfigValue("SqlDatabase");

            // Create the Connection string
            EntityConnectionString = CreateConnectionString(true, serverName, databaseName);
            SqlConnectionString = CreateConnectionString(false, serverName, databaseName);
            FileGroupAdded = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, 0, 0);
        }

        /// <summary>
        /// Gets FileGroupAdded.
        /// </summary>
        public static DateTime FileGroupAdded { get; private set; }

        /// <summary>
        /// Gets EntityConnectionString.
        /// </summary>
        public static string EntityConnectionString { get; private set; }

        /// <summary>
        /// Gets SqlConnectionString.
        /// </summary>
        public static string SqlConnectionString { get; private set; }

        /// <summary>
        /// Gets RssOutputFolder.
        /// </summary>
        public static string RssOutputFolder
        {
            get
            {
                return ReadConfigValue("RssOutputFolder");
            }
        }

        /// <summary>
        /// Gets a value indicating whether argIsSilent.
        /// </summary>
        public static bool ArgIsSilent { get; private set; }

        /// <summary>
        /// Parse the application arguments and set their values to the variables
        /// </summary>
        /// <param name="args">
        /// The args are the application command line arguments
        /// </param>
        public static void ParseArgs(string[] args)
        {
            // Parse Arguments
            for (int i = 0; i < args.Length; i++)
            {
                // Initialize the variable that holds the adjacent argument to the switch
                string adjacentArgument = string.Empty;

                // If there are multiple arguments, check for the value
                if (i + 1 < args.Length)
                {
                    if (args[i + 1][0] != '-')
                    {
                        adjacentArgument = args[i + 1];
                    }
                }

                // Set variables from arguments
                if (args[i] == "-silent")
                {
                    ArgIsSilent = true;
                }

                // SAMPLE: If argument is "-server 10.177.35.100" then set variable = "10.177.35.100"
                ////if (args[i] == "-argument") { variable = adjacentArgument; }
            }
        }

        /// <summary>
        /// Return specified value from conf.xml
        /// </summary>
        /// <param name="xmlElement">Name of the XML Element of which value shall be returned</param>
        /// <returns>
        /// The config value requested
        /// </returns>
        public static string ReadConfigValue(string xmlElement)
        {
            string configValue = null;

            // Create the query
            var config = from n in XElement.Load("Conf.xml").Elements().Descendants(xmlElement)
                         select n;

            // Execute the query
            foreach (var line in config)
            {
                configValue = line.Value;
            }

            return configValue;
        }

        /// <summary>
        /// Creates a Connection string from Conf.xml
        /// </summary>
        /// <param name="isEntityConnection">
        /// The is Entity Connection.
        /// </param>
        /// <param name="serverName">
        /// The server Name.
        /// </param>
        /// <param name="databaseName">
        /// The database Name.
        /// </param>
        /// <returns>
        /// A fully functional Connection string
        /// </returns>
        public static string CreateConnectionString(bool isEntityConnection, string serverName, string databaseName)
        {
            ////////////////////////////////////////////////////////
            // Rest of this Method was grabbed from MSDN
            // http://msdn.microsoft.com/en-us/library/bb738533.aspx

            // Specify the provider name, server and database.
            const string providerName = "System.Data.SqlClient";

            // Initialize the connection string builder for the underlying provider.
            SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
                                                        {
                                                            // Set the properties for the data source.
                                                            DataSource = serverName,
                                                            InitialCatalog = databaseName,
                                                            IntegratedSecurity = true,
                                                            MultipleActiveResultSets = true
                                                        };

            // Build the SqlConnection connection string.
            string providerString = sqlBuilder.ToString();

            // Initialize the EntityConnectionStringBuilder.
            EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder
                                                              {
                                                                  // Set the provider name.
                                                                  Provider = providerName,

                                                                  // Set the provider-specific connection string.
                                                                  ProviderConnectionString = providerString,

                                                                  // Set the Metadata location.
                                                                  Metadata = @"res://*/Data.Dir2RssLinq2Ent.csdl|" +
                                                                             @"res://*/Data.Dir2RssLinq2Ent.ssdl|" +
                                                                             @"res://*/Data.Dir2RssLinq2Ent.msl"
                                                              };

            // Return the Connection string
            if (isEntityConnection)
            {
                return entityBuilder.ToString();
            }
            else
            {
                return sqlBuilder.ToString();
            }
        }
    }
}
